
import {
  list
} from '../../../../api/product'

import {
  list as tagList
} from '../../../../api/tag'
const util = require('../../../../utils/util')

let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info:{
      type:Object
    }
},
  lifetimes:{
    attached(){
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
        this.setData({
          tagIds:this.data.tagIds
        })
      
      console.log(this.data.tagIds)
      this.loadTags()
//       this.loadProduct()
    }
      
  },
  /**
   * 组件的初始数据
   */
  data: {
    tagIds: [],
    tagId: '',
    scrollTop: '',
    tags: [],
    allHeight: 'height:600rpx',
    length: '',
    pageId: 18,
    componentsList: [],
    productList: [],
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    pageNum: 1,
    pageSize: 20,
  },

  attached() {
    var that = this;

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  created() {

  },
  ready(){
    let query = wx.createSelectorQuery().in(this)
    query.select('#title').boundingClientRect()
    query.exec(res => {
      console.log(res)
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {

    loadTags() {
      var _this = this;
      tagList({
        pageNum: this.data.pageNum,
        pageSize: 5,
        mchId: app.globalData.mchId,
        type: '3',
        tagIds: this.data.tagIds,
      }).then(res => {
        _this.data.tags = res.data.data
        _this.data.tagId = this.data.tags[0].id
        _this.setData({
          tagId: _this.data.tagId,
          tags: _this.data.tags
        })

        // setTimeout(() => {
          _this.data.pageNum = 0
          _this.data.productList = []    
          _this.loadProduct()
        // }, 1000)
  
  
      })
  
    },
    //  tab切换逻辑
    swichNav: function (e) {
      console.log(e.currentTarget.dataset.id)
      this.data.pageNum = 0
      this.data.tagId = e.currentTarget.dataset.id
      this.data.productList = []
      this.loadProduct()
      var that = this;
      if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        that.setData({
          tagId: this.data.tagId,
          currentTab: e.target.dataset.current
        })
      }
    },
    loadProduct() {
      var _this = this
      _this.data.pageNum += 1
      list({
        tagIds:[this.data.tagId],
        mchId: app.globalData.mchId,
        pageSize: _this.data.pageSize,
        pageNum: _this.data.pageNum,
      }).then(res => {
        if (_this.data.pageNum == 1) {
          _this.data.productList = []
        }
        res.data.data.forEach(item => {
          console.log(item)
          item.thumbnail = util.thumbnail(item.thumbnail, 500, 500)
          _this.data.productList.push(item)
        })       
        // if (_this.data.productList.length == 0) {
        //   _this.data.allHeight = 'height:' + 600 + 'rpx';
        // }else{
        //   _this.data.allHeight = 'height:' + (_this.data.productList.length * 292) + 'rpx'
        // }
        _this.setData({
          // allHeight: _this.data.allHeight,
          productList: _this.data.productList,
          pageNum: _this.data.pageNum
        })

      })
    },
    bindChange: function (e) {

      this.data.tagId = this.data.tags[e.detail.current].id
      this.data.pageNum = 0
      this.data.productList = []
      this.loadProduct()
      this.setData({
        currentTab: e.detail.current
      });

    },
    //+++++++++++上拉加载+++++++++
    onReachBottom: function () {
      this.loadProduct()

    },
    downRefresh: function () {
      this.data.productList = []
      this.data.pageSize = 20
      this.data.pageNum = 0
      this.loadProduct()
      this.setData({
        productList: this.data.productList,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      })
    },
  }
})